async function login() {
    const account = document.getElementById('login-username').value;
    const password = document.getElementById('login-password').value;
    const errorMessage = document.getElementById('login-error');
    const messageBox = document.getElementById('message');

    if (account === "" || password === "") {
        errorMessage.textContent = "所有字段都是必填的";
        return;
    }

    try {
        const response = await fetch('http://localhost:8888/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ account, password })
        });

        const result = await response.json();

        if (result.code === 200) {
            errorMessage.textContent = "";
            messageBox.textContent = result.msg;
            messageBox.className = 'message success';
            messageBox.style.display = "block";

            // 保存用户信息和令牌到本地存储
            const user = { account, token: result.data };
            localStorage.setItem('userInfo', JSON.stringify(user));

            // 登录成功后重定向到主页
            setTimeout(() => {
                window.location.href = 'blog_list.html';
            }, 1000);
        } else {
            errorMessage.textContent = "";
            messageBox.textContent = result.msg;
            messageBox.className = 'message error';
            messageBox.style.display = "block";
        }
    } catch (error) {
        errorMessage.textContent = "";
        messageBox.textContent = "网络错误，请稍后重试";
        messageBox.className = 'message error';
        messageBox.style.display = "block";
    }
}
